package com.myTimeUtils.android.TimeTracker.Schedule;
/**
 * This class models a period of time. It tracks start time, end time, and can receive a simple name. 
 *
 * @author Joseph Walker
 */
public class Period implements Comparable{
	private Time end;
	private String name;
	private Time start;
	/**
	 * Constructs a period using the given information.
	 *
	 * @param end				The end of the period.
	 * @param name				The name of the period.
	 * @param start				The start of the period
	 */
	public Period(Time end, String name, Time start){
		this.setEnd(end);
		this.setName(name);
		this.setStart(start);
	}
	/**
	 * Returns true if the given time occurs before the start of the period.
	 *
	 * @param time				The time to check against the period start
	 * @return 				True if the time occurs before the period start
	 */
	public boolean beforePeriod(Time time){
		return this.getStart().compareTo(time) > 0;
	}
	/**
	 * Compares the given object to this one. Returns 1 if this object starts first.
	 *
	 * @param object			The object to compare to
	 * @return				1 if this object occurs first.
	 */
	public int compareTo(Object object){
		Period p = (Period)object;
		return this.getStart().compareTo(p.getStart());
	}
	/**
	 * Returns true if the given object is a period and if any point of time in that period
	 * falls within this period.
	 *	
	 * @param o				The object to compare to this one
	 * @return				True if any point in the given period falls within this one. 
	 */
	public boolean equals(Object o){
		if(o instanceof Period) return equals((Period)o);
		return false;
	}
	private boolean equals(Period period){
		return this.isInPeriod(period.getStart()) || this.isInPeriod(period.getEnd()) 
		       || period.isInPeriod(this.getStart()) || period.isInPeriod(this.getEnd()); 
	}	 
	/**
	 * Returns the end time of this period.
	 *
	 * @return				The end time of this period
	 */
	public Time getEnd(){ return this.end; }
	/**
	 * Returns the length of the period.
	 *
	 * @return				The length of the period
	 */
	public String getLength(){
		return this.timeString(this.getEnd().getTimeDifference(this.getStart()));
	}
	/**
	 * Returns the name of this period.
	 *
	 * @return				The name of this period
	 */
	public String getName(){ return this.name; }
	/**
	 * Returns the start time of this period.
	 *
	 * @return				The start of this period
	 */
	public Time getStart(){ return this.start; }
	/**
	 * Determines if the given time is within this period. 
	 *
	 * @param time				The time to test
	 * @return				True if the given time is in the period
	 */
	public boolean isInPeriod(Time time){
		return this.getEnd().compareTo(time) > 0 && this.getStart().compareTo(time) < 0; 
	}
	/**
	 * Finds the remaining time in the period from the given time. 
	 *
	 * @param time				The time to check from
	 * @return				The time remaining in the period from the given time
	 */	
	public String remainingTime(Time time){
		//TODO error checking is time in period?		
		return this.timeString(this.getEnd().getTimeDifference(time));
	}
	/**
	 * Sets the end time of this period.
	 *
	 * @param end				The end of this period
	 */
	public void setEnd(Time end){ this.end = end; }
	/**
	 * Sets the name of this period.
	 *
	 * @param name				The name of this period
	 */
	public void setName(String name){ this.name = name; }
	/**
	 * Sets the start of this period.
	 *
	 * @param start				The start of this period
	 */
	public void setStart(Time start){ this.start = start; }
	private String timeString(int vals[]){
		String out = "";
		if(vals[0] != 0) out += vals[0] + ":";
		if(vals[1] > 9) out += vals[1] + ":";
		else out += "0" + vals[1] + ":";
		if(vals[2] > 9) out += vals[2] + ":";
		else out += "0" + vals[2] + ":";
		if(vals[3] > 9) out += vals[3] + "";
		else out += "0" + vals[3] + "";
		return out;
	}
	/**
	 * Returns a string representation of this period. 
	 *
	 *@return				A string representation of this period
	 */
	public String toString(){ return this.getName() + ": " + this.getStart() + " - " + this.getEnd(); }
}
